package com.wltt.product.frfront.service;

import com.wltt.product.frfront.config.exception.BusinessException;
import com.wltt.product.frfront.holder.SystemHolder;
import com.wltt.product.frfront.mapper.SystemUserMapper;
import com.wltt.product.frfront.model.ResultData;
import com.wltt.product.frfront.model.po.SystemUserPO;
import com.wltt.product.frfront.model.vo.*;
import com.wltt.product.frfront.service.base.BaseServiceImpl;
import com.wltt.product.frfront.utils.CodeGenerateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * @ClassName: SystemUserServiceImpl
 * @Description: 逻辑服务类<br />
 * 不做业务校验，参数校验统一放到前置的Service中
 * @Date: 2025-07-03 18:01:51
 * @Author yanggl
 */
@Service(value = "systemUserService")
public class SystemUserServiceImpl extends BaseServiceImpl<SystemUserPO, Long> implements SystemUserService {

    private SystemUserMapper systemUserMapper;

    //构造方法
    public SystemUserServiceImpl(SystemUserMapper systemUserMapper) {
        super(systemUserMapper);
        this.systemUserMapper = systemUserMapper;
    }

    /**
     * 登录
     *
     * @param loginREQ
     * @return
     */
    @Override
    public ResultData login(LoginREQ loginREQ) {
        if (null == loginREQ.getUserAccount()) {
            throw new BusinessException("用户名无效", HttpStatus.BAD_REQUEST);
        }
        loginREQ.setUserAccount(loginREQ.getUserAccount().trim());
        if (!StringUtils.hasLength(loginREQ.getUserAccount())) {
            throw new BusinessException("用户名无效", HttpStatus.BAD_REQUEST);
        }

        if (!StringUtils.hasLength(loginREQ.getUserPassword())) {
            throw new BusinessException("密码无效", HttpStatus.BAD_REQUEST);
        }

        SystemUserPO systemUserPO = systemUserMapper.queryByUserAccount(loginREQ.getUserAccount());
        if (null == systemUserPO) {
            throw new BusinessException("账户密码错误", HttpStatus.BAD_REQUEST);
        }

        if (!systemUserPO.getUserPassword().equals(loginREQ.getUserPassword())) {
            throw new BusinessException("账户密码错误", HttpStatus.BAD_REQUEST);
        }

        String loginCode = CodeGenerateUtil.generateUUID();
        SystemHolder.setLoginCode(loginCode);
        return ResultData.success(loginCode);
    }

    /**
     * 查询所有系统用户
     *
     * @return
     */
    @Override
    public ResultData allSystemUser() {
        List<SystemUserPO> systemUserPOList = systemUserMapper.queryAll();
        List<SystemUserDetailRSP> rsps = Optional.ofNullable(systemUserPOList).orElse(new ArrayList<>())
                .stream().map(po -> {
                    SystemUserDetailRSP rsp = new SystemUserDetailRSP();
                    BeanUtils.copyProperties(po, rsp);
                    return rsp;
                }).collect(Collectors.toList());
        return ResultData.success(rsps);
    }

    /**
     * 系统用户详情
     *
     * @param systemUserDetailREQ
     * @return
     */
    @Override
    public ResultData systemUserDetail(SystemUserDetailREQ systemUserDetailREQ) {
        if (null == systemUserDetailREQ || null == systemUserDetailREQ.getId()) {
            throw new BusinessException("未指定用户", HttpStatus.BAD_REQUEST);
        }
        SystemUserPO systemUserPO = systemUserMapper.queryById(systemUserDetailREQ.getId());
        if (null == systemUserPO) {
            throw new BusinessException("用户不存在", HttpStatus.BAD_REQUEST);
        }

        SystemUserDetailRSP systemUserDetailRSP = new SystemUserDetailRSP();
        BeanUtils.copyProperties(systemUserPO, systemUserDetailRSP);
        return ResultData.success(systemUserDetailRSP);
    }

    /**
     * 新增系统用户
     *
     * @param addSystemUserREQ
     * @return
     */
    @Override
    @Transactional
    public ResultData addSystemUser(AddSystemUserREQ addSystemUserREQ) {
        if (null == addSystemUserREQ.getUserAccount()) {
            throw new BusinessException("用户名无效", HttpStatus.BAD_REQUEST);
        }
        addSystemUserREQ.setUserAccount(addSystemUserREQ.getUserAccount().trim());
        if (!StringUtils.hasLength(addSystemUserREQ.getUserAccount())) {
            throw new BusinessException("用户名无效", HttpStatus.BAD_REQUEST);
        }

        if (!StringUtils.hasLength(addSystemUserREQ.getUserPassword())) {
            throw new BusinessException("密码无效", HttpStatus.BAD_REQUEST);
        }

        if (null == addSystemUserREQ.getUserType()) {
            throw new BusinessException("类型", HttpStatus.BAD_REQUEST);
        }

        SystemUserPO systemUserPO = systemUserMapper.queryByUserAccount(addSystemUserREQ.getUserAccount());
        if (null != systemUserPO) {
            throw new BusinessException("用户名已存在", HttpStatus.BAD_REQUEST);
        }
        systemUserPO = new SystemUserPO();
        BeanUtils.copyProperties(addSystemUserREQ, systemUserPO);
        systemUserPO.setUserCode(CodeGenerateUtil.generateTimestampRandom(1));
        systemUserMapper.insertSelective(systemUserPO);
        return ResultData.success();
    }

    /**
     * 更新系统用户
     *
     * @param updateSystemUserREQ
     * @return
     */
    @Override
    @Transactional
    public ResultData updateSystemUser(UpdateSystemUserREQ updateSystemUserREQ) {
        if (null == updateSystemUserREQ || null == updateSystemUserREQ.getId()) {
            throw new BusinessException("未指定用户", HttpStatus.BAD_REQUEST);
        }

        if (null == updateSystemUserREQ.getUserAccount()) {
            throw new BusinessException("用户名无效", HttpStatus.BAD_REQUEST);
        }
        updateSystemUserREQ.setUserAccount(updateSystemUserREQ.getUserAccount().trim());
        if (!StringUtils.hasLength(updateSystemUserREQ.getUserAccount())) {
            throw new BusinessException("用户名无效", HttpStatus.BAD_REQUEST);
        }

        if (null == updateSystemUserREQ.getUserType()) {
            throw new BusinessException("类型", HttpStatus.BAD_REQUEST);
        }

        SystemUserPO systemUserPO_old = systemUserMapper.queryById(updateSystemUserREQ.getId());
        if (null == systemUserPO_old) {
            throw new BusinessException("用户不存在", HttpStatus.BAD_REQUEST);
        }

        SystemUserPO systemUserPO = systemUserMapper.queryByUserAccount(updateSystemUserREQ.getUserAccount());
        if (null != systemUserPO && systemUserPO.getId() != updateSystemUserREQ.getId()) {
            throw new BusinessException("用户名已存在", HttpStatus.BAD_REQUEST);
        }

        systemUserPO_old.setUserAccount(updateSystemUserREQ.getUserAccount());
        systemUserPO_old.setUserType(updateSystemUserREQ.getUserType());
        systemUserMapper.updateById(systemUserPO_old);

        return ResultData.success();
    }

    /**
     * 更新密码
     *
     * @param updatePasswordREQ
     * @return
     */
    @Override
    @Transactional
    public ResultData updatePassword(UpdatePasswordREQ updatePasswordREQ) {

        if (null == updatePasswordREQ || null == updatePasswordREQ.getUserAccount()) {
            throw new BusinessException("未指定用户", HttpStatus.BAD_REQUEST);
        }

        if (!StringUtils.hasLength(updatePasswordREQ.getUserPassword())) {
            throw new BusinessException("请输入原密码", HttpStatus.BAD_REQUEST);
        }

        if (!StringUtils.hasLength(updatePasswordREQ.getNewPassword())) {
            throw new BusinessException("请输入新密码", HttpStatus.BAD_REQUEST);
        }

        if (!updatePasswordREQ.getNewPassword().equals(updatePasswordREQ.getConfirmPassword())) {
            throw new BusinessException("两次输入密码不一致", HttpStatus.BAD_REQUEST);
        }

        SystemUserPO systemUserPO = systemUserMapper.queryByUserAccount(updatePasswordREQ.getUserAccount());
        if (null == systemUserPO) {
            throw new BusinessException("用户不存在", HttpStatus.BAD_REQUEST);
        }

        if (!systemUserPO.getUserPassword().equals(updatePasswordREQ.getUserPassword())) {
            throw new BusinessException("原密码错误", HttpStatus.BAD_REQUEST);
        }

        systemUserPO.setUserPassword(updatePasswordREQ.getNewPassword());
        systemUserMapper.updateById(systemUserPO);

        return ResultData.success();
    }

    /**
     * 删除系统用户
     *
     * @param deleteSystemUserREQ
     * @return
     */
    @Override
    @Transactional
    public ResultData deleteSystemUser(DeleteSystemUserREQ deleteSystemUserREQ) {
        if (null == deleteSystemUserREQ || null == deleteSystemUserREQ.getId()) {
            throw new BusinessException("未指定用户", HttpStatus.BAD_REQUEST);
        }
        systemUserMapper.deleteById(deleteSystemUserREQ.getId());
        return ResultData.success();
    }
}